<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <template #header>
          <el-row type="flex" justify="end">
            <el-button size="small" type="primary" @click="addFn">添加员工</el-button>
          </el-row>
        </template>
        <!-- 表格 -->
        <el-table border :data="employeesData">
          <el-table-column label="用户" prop="username" />
          <el-table-column label="邮箱" prop="email" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button size="small" type="primary" @click="rolesFn(row.id)">设置角色</el-button>
              <el-button size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="center">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="limit"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <!-- 添加编辑弹窗 -->
      <el-dialog :title="diaData.id?'编辑员工':'添加员工'" :visible="showDialog" @close="clonse">
        <el-form ref="form" label-width="120px" :model="diaData" :rules="rules">
          <el-form-item label="用户名" prop="username">
            <el-input v-model.trim="diaData.username" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model.trim="diaData.email" />
          </el-form-item>
          <el-form-item v-if="diaData.id?false:true" label="密码" prop="password">
            <el-input v-model.trim="diaData.password" type="password" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button size="small" type="primary" @click="submit">确认</el-button>
            <el-button size="small" @click="clonse">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
      <!-- 分配角色弹窗 -->
      <RolesEmploy ref="roles" :roles-dailog.sync="roleDialog" />
    </div>
  </div>
</template>

<script>
import { getEmployeesList, getEmployeesCount, addEmployees, getEmployeesInfo, editEmployeesInfo, delEmployeesInfo } from '@/api/employees'
import RolesEmploy from './components/roles-employ.vue'
export default {
  components: { RolesEmploy },
  data() {
    return {
      limit: 5, // 每页的条数
      start: 0, // 开始获取的位置
      employeesData: [], // 获取的每页条数列表
      employeesList: [], // 所有的列表数据
      total: 0, // 总数量
      showDialog: false, // 添加编辑弹窗
      diaData: {
        username: '',
        email: '',
        password: ''
      }, // 添加的数据
      roleDialog: false, // 分配角色弹窗
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/, message: '邮箱格式不正确', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }

        ]
      }
    }
  },
  created() {
    this.getEmployeesLIst()
  },
  methods: {
    // 分配角色
    rolesFn(id) {
      this.roleDialog = true
      this.$refs.roles.getRolesId(id)
    },
    // 删除
    delFn(id) {
      this.$confirm('是否确认删除?', '提示', {
        type: 'warning'
      }).then(async() => {
        await delEmployeesInfo(id)
        if (this.start !== 0 && this.employeesData.length === 1) {
          const start = (this.total / this.limit)
          this.currentChange(start)
        } else {
          this.getEmployeesLIst()
        }
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch((err) => {
        console.log('取消', err)
      })
    },
    // 确认添加/编辑
    async submit() {
      await this.$refs.form.validate()
      if (this.diaData.id) {
        await editEmployeesInfo(this.diaData)
      } else {
        await addEmployees(this.diaData)
      }
      this.getEmployeesLIst()
      this.$message({
        message: this.diaData.id ? '更改成功' : '添加成功',
        type: 'success'
      })
      this.clonse()
    },
    // 编辑按钮
    async editFn(id) {
      const res = await getEmployeesInfo(id)
      // console.log(res)
      this.diaData = res.data
      this.showDialog = true
    },
    // 关闭弹窗
    clonse() {
      this.showDialog = false
      this.diaData = {
        username: '',
        email: '',
        password: ''
      }
      this.$refs.form.resetFields()
    },
    // 添加员工
    addFn() {
      this.showDialog = true
    },
    // 获取列表和数量
    async getEmployeesLIst() {
      const res = await getEmployeesList({ _limit: this.limit, _start: this.start })
      const sult = await getEmployeesCount()
      this.employeesData = res.data
      this.total = sult.data
    },
    // 分页
    currentChange(start) {
      this.start = 0 + (start - 1) * this.limit
      this.getEmployeesLIst()
    }
  }

}
</script>

<style>

</style>
